package com.xmcc.utill;

import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.ShardedJedis;

import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class JedisUtils {
    @Autowired
    private ShardedJedisPoolUtil shardedJedisPoolUtil; //注入util工具类对象：

    public void setUserAcl(Integer userId,Set<Integer> aclIds){
        //获得redis连接：
        ShardedJedis resource = shardedJedisPoolUtil.getResource();
        //1.序列化为字符串 json
        try{
            resource.setex(Constant.USER_ACL_PRE+userId,600,JsonUtil.object2string(aclIds));
        }catch(Exception e){
            log.info("缓存用户权限异常.....e:{}",e);
        }finally {
            shardedJedisPoolUtil.closeJedis(resource);
        }
    }

    public Set<Integer> getUserAcl(Integer userId){
        //获得redis连接：
        ShardedJedis resource = shardedJedisPoolUtil.getResource();
        String s = resource.get(Constant.USER_ACL_PRE+userId);
        if (s==null){
            return null;
        }
        shardedJedisPoolUtil.closeJedis(resource); //应该写在finally里
        return JsonUtil.string2object(s, new TypeReference<Set<Integer>>() {
            @Override
            public int compareTo(TypeReference<Set<Integer>> o) {
                return super.compareTo(o);
            }
        });
    }

    public void removeUserList(List<Integer> userIds){
        ShardedJedis resource = shardedJedisPoolUtil.getResource();
        //授权是根据角色授权  需要用角色id把用户查询出来 在删除
        // resource.del()
    }
}
